home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / games / larn12s.arc / TERMCAP.ARC / WORMS.C < prev   
C/C++ Source or Header  |  1987-10-28  |  6KB  |  232 lines

  1. /*
  2.  * History : Written by tjalk.cs.vu.nl  ( I got no name from the article) 
  3.  *
  4.  * Hacked to use termcap by: Josh Siegel (josh@hi.unm@hc.dspo.gov) 
  5.  *
  6.  *
  7.  * Please note, The hack that handles loading the atributes into the system
  8.  * is exactly that... a hack.  I had little time and little want to make
  9.  * it better. 
  10.  *
  11.  *
  12.  * --Josh Siegel 
  13.  *
  14.  */
  15. #define MaxNrOfWorms 32        /* You can't alter this without altering
  16.                  * the zoo!  (Zoo: see somewhere else in
  17.                  * this program)      */
  18. #define MaxLength    48        /* This really is long enough! */
  19. long _stksize=65536;
  20. int        ScreenWidth;
  21. int        ScreenHeigth;
  22. char            cm[16], cl[16];
  23.  
  24.  
  25. #include <stdio.h>
  26.  
  27.  
  28. int        wormlength = 20;/* 20 by default, can be redefined by
  29.                  * -l<nr> */
  30. int        nrofworms = 3;    /* 3 by default, can be redefined by
  31.                  * -n<nr> */
  32. char           *message = "                W O R M S ! ! !";
  33. /* message can be redifined by -m<string> */
  34.  
  35. int             x_direction[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  36. int             y_direction[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  37.  
  38. int    screen[100][100];
  39.  
  40.  
  41.  
  42. main(argc, argv)
  43.   char          **argv;
  44.  
  45. {
  46.   char    wormx[MaxNrOfWorms][MaxLength], wormy[MaxNrOfWorms][MaxLength];
  47.   int             dir[MaxNrOfWorms], *direction;
  48.   int        ptr1, ptr2, x, y, i, j;
  49.   long            dummy;
  50.   char           *pos();
  51.  
  52.  
  53.   setbuf(stdout, NULL);
  54.   /* First test for any flags... */
  55.  
  56.   for (i = 1; i < argc; i++)
  57.     if (argv[i][0] == '-') {
  58.       switch (argv[i][1]) {
  59.       case 'L':
  60.     argv[i][1]='l';
  61.       case 'l':
  62.     if (
  63.         sscanf(argv[i], "-l%d", &wormlength) == 0 ||
  64.         wormlength < 2 || wormlength > MaxLength) {
  65.       puts("Bad length.");
  66.       exit(1);
  67.     } break;
  68.       case 'N':
  69.     argv[i][1]='n';
  70.       case 'n':
  71.     if (
  72.         sscanf(argv[i], "-n%d", &nrofworms) == 0 ||
  73.         nrofworms < 1 || nrofworms > MaxNrOfWorms) {
  74.       puts("Bad number of worms.");
  75.       exit(1);
  76.     } break;
  77.       case 'M':
  78.       case 'm':
  79.     message = argv[i] + 2;
  80.     break;
  81.       default:
  82.     printf(" -%c: bad option.\n", argv[i][1]);
  83.     exit(1);
  84.       }
  85.     } else {
  86.       printf("Syntax: %s [ -l<nr> ] [ -n<nr> ] \
  87. [ -m<message> ]\n", argv[0]);
  88.       exit(1);
  89.     }
  90.  
  91.   /* Now let's initialize !! */
  92.  
  93.   initsys();
  94.   clear_screen();
  95.   printf("%s%s", pos(0, 22), message);
  96.  
  97.   for (j = 0; j < nrofworms; j++) {
  98.     for (i = 0; i < wormlength; i++)
  99.       wormx[j][i] = wormy[j][i] = 0;
  100.     dir[j] = 1;            /* direction of worm j */
  101.   }
  102.  
  103.   screen[0][0] = nrofworms * wormlength;
  104.   ptr1 = ptr2 = 0;
  105.   /* They point to the front and the back of a worm, respectively. */
  106.  
  107.  
  108.   /* init randomizer... I hope this will work, it works on UNIX */
  109.   srand((int) time(&dummy));
  110.  
  111.  
  112.  
  113.   while (1) {
  114.     ptr2 = (ptr1 + 1) % wormlength;
  115.     /* Take the next part of each worm. */
  116.  
  117.     for (i = 0; i < nrofworms; i++) {
  118.       register int    enough = 0;
  119.  
  120.       x = wormx[i][ptr2];
  121.       y = wormy[i][ptr2];
  122.       /*
  123.        * x and y are the coordinates of the tail of worm i, which has
  124.        * to be removed.             
  125.        */
  126.       if ((--screen[x][y]) == 0)
  127.     printf("%s ", pos(x, y));
  128.       /*
  129.        * It will only be removed when there are no other "parts" of
  130.        * worms on that spot on the screen.  
  131.        */
  132.  
  133.       /* Now put a head somewhere. */
  134.       /*
  135.        * We'll modify the direction just until we've got a correct
  136.        * direction (so that the worm will not cross or  overlap another
  137.        * worm or itself, nor will run of the screen),  or that we've had
  138.        * enough,  since than there probably is no such direction. 
  139.        */
  140.  
  141.       direction = &dir[i];
  142.  
  143.       do
  144.     *direction = (*direction + (rand()) % 3 + 7) % 8;
  145.       while ((x = wormx[i][ptr1] + x_direction[*direction]) < 0 ||
  146.          x > ScreenWidth - 1 ||
  147.          (y = wormy[i][ptr1] + y_direction[*direction]) < 0 ||
  148.          y > ScreenHeigth - 2 ||
  149.          ((screen[x][y] != 0 || cross(x, y, *direction))
  150.           && enough++ < 19));
  151.  
  152.       if (enough >= 19) {    /* We can't move this poor worm */
  153.     x = wormx[i][ptr1];    /* old coordinates  */
  154.     y = wormy[i][ptr1];
  155.       }
  156.       /*
  157.        * x and y now contain the new coordinates of the worms head. Let's
  158.        * just place it on the screen (and in memory) 
  159.        */
  160.       wormx[i][ptr2] = x;
  161.       wormy[i][ptr2] = y;
  162.       printf("%s%c", pos(x, y),
  163.          "*#Ox@~.+!&%$'`=-oXwW()^~*#x0@+./"[i]);
  164.       /* Welcome to the zoo!! ^^^^ */
  165.       screen[x][y]++;
  166.     }
  167.     ptr1 = ptr2;
  168.   }
  169. }
  170.  
  171.  
  172.  
  173.  
  174. cross(x, y, d)
  175. {                /* keeps two worms from crossing */
  176.   if (d & 1 == 0)
  177.     return 0;            /* worm moving diagonally? */
  178.  
  179.   return (screen[x][y - y_direction[d]] && screen[x - x_direction[d]][y]);
  180. }
  181.  
  182. /*
  183.  * I assume there are betters ways of doing this but since I had no
  184.  * interest in spending more then 5 minutes making this use termcap ... 
  185.  */
  186.  
  187. initsys()
  188. {
  189.   static char     hasrun = 0, bp[1024], bp2[1024], *tgetstr(), *p1;
  190.   char           *tmp, *getenv();
  191.  
  192.   if((tmp=getenv("TERM"))==NULL)
  193.     tmp="st52";
  194.   tgetent(bp, tmp);
  195.   strcpy(bp2, bp);
  196.   p1 = bp2;
  197.   tmp = tgetstr("cl", &p1);
  198.   strcpy(cl, tmp);
  199.   strcpy(bp2, bp);
  200.   p1 = bp2;
  201.   tmp = tgetstr("cm", &p1);
  202.   strcpy(cm, tmp);
  203.   strcpy(bp2, bp);
  204.   p1 = bp2;
  205.   if ((ScreenHeigth = tgetnum("li", &p1) - 1) > 99) {
  206.     printf("I cannot handle a terminal that has more then 99 lines\n");
  207.     exit(0);
  208.   }
  209.   strcpy(bp2, bp);
  210.   p1 = bp2;
  211.   if ((ScreenWidth = tgetnum("co", &p1) - 1) > 99) {;
  212.     printf("I cannot handle a terminal that has more then 99 rows\n");
  213.     exit(0);
  214.   }
  215. }
  216.  
  217. char           *
  218. pos(x, y)
  219. {
  220.   char           *ret, *tgoto();
  221.  
  222.   ret = tgoto(cm, x, y);
  223.   return ret;
  224. }
  225.  
  226.  
  227.  
  228. clear_screen()
  229. {
  230.   puts(cl);
  231. }
  232.